package com.twxm.breed.runner;

import com.aliyuncs.exceptions.ClientException;
import com.twxm.breed.entity.DeviceInfo;
import com.twxm.breed.entity.FishPond;
import com.twxm.breed.entity.Monitor;
import com.twxm.breed.iot.ALiYunIOTAPI;
import com.twxm.breed.iot.OxygenTaskManager;
import com.twxm.breed.iot.TimerTask;
import com.twxm.breed.service.*;
import com.twxm.breed.util.WXMsgSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    private MonitorService monitorService;
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private LoginInfoService loginInfoService;
    @Autowired
    private EventLogService eventLogService;
    @Autowired
    private FishPondService fishPondService;
    @Autowired
    private WaterFactorService waterFactorService;
    @Autowired
    private WeatherFactorService weatherFactorService;
    @Autowired
    private DissolvedOxygenService dissolvedOxygenService;
    @Autowired
    private TimerManager timerManager;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //如果设备是开启的，操控阿里云设备端开启
        List<FishPond> ponds = fishPondService.getAll();
        List<DeviceInfo> deviceInfos = deviceInfoService.getAll();
        ponds.forEach(pond->{
            DeviceInfo o = deviceInfoService.getOxygenSensorByPondId(pond.getPondId());
            if(o != null){
                timerManager.addTask(pond, 5000,  o.getMinute() * 60000L);
                System.out.println("AutoGetAdd:" + o.getMinute() + pond.getPondId());
                return;
            }
            DeviceInfo w = deviceInfoService.getWaterSensorByPondId(pond.getPondId());
            if(w != null){
                timerManager.addTask(pond, 5000,  w.getMinute() * 60000L);
                System.out.println("AutoGetAdd:" + o.getMinute() + pond.getPondId());
                return;
            }
            DeviceInfo we = deviceInfoService.getWeatherSensorByPondId(pond.getPondId());
            if(we != null){
                timerManager.addTask(pond, 5000,  we.getMinute() * 60000L);
                System.out.println("AutoGetAdd:" + o.getMinute() + pond.getPondId());
                return;
            }
            return;
        });
        deviceInfos.forEach(deviceInfo -> {
            if (deviceInfo.getStatus() == 1){
                try {
                    ALiYunIOTAPI.getInstance().setDeviceOpen(deviceInfo.getCategoryInfo().getProductKey(), deviceInfo.getDeviceId());
                    //查询是否有溶氧监控任务
                    if (deviceInfo.getCategoryInfo().getName().equals("增氧泵")){
                        Monitor monitor = monitorService.getMonitorDeviceId(deviceInfo.getDeviceId());
                        if (monitor != null){
                            DeviceInfo oxygenDevice = deviceInfoService.getOxygenSensorByPondId(deviceInfo.getFishPond().getPondId());
                            //获取monitor中溶解氧的上下限
                            float upper = monitor.getUpper();
                            float lower = monitor.getLower();
                            OxygenTaskManager.getInstance().addPond(deviceInfo.getFishPond().getPondId(), oxygenDevice, deviceInfo, upper, lower);
                        }
                    }
                } catch (ClientException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        WXMsgSender.loginInfoService = loginInfoService;
        OxygenTaskManager.eventLogService = eventLogService;
        OxygenTaskManager.fishPondService = fishPondService;
        TimerTask.deviceInfoService = deviceInfoService;

        TimerTask.waterFactorService = waterFactorService;
        TimerTask.weatherFactorService = weatherFactorService;
        TimerTask.dissolvedOxygenService = dissolvedOxygenService;
    }
}
